package com.fatcatlab.tanchess;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.FixedStepPhysicsWorld;

import android.bluetooth.BluetoothAdapter;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.badlogic.gdx.math.Vector2;

public class BTGameScene extends GameScene {

	// Message types sent from the BluetoothChatService Handler
	public static final int MESSAGE_STATE_CHANGE = 1;
	public static final int MESSAGE_READ = 2;
	public static final int MESSAGE_WRITE = 3;
	public static final int MESSAGE_DEVICE_NAME = 4;
	public static final int MESSAGE_TOAST = 5;

	// Key names received from the BluetoothChatService Handler
	public static final String DEVICE_NAME = "device_name";
	public static final String TOAST = "toast";

	// Intent request codes
	private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
	private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
	private static final int REQUEST_ENABLE_BT = 3;
	
	
	// Name of the connected device
    private String mConnectedDeviceName = null;
    // Array adapter for the conversation thread
    private ArrayAdapter<String> mConversationArrayAdapter;
    // String buffer for outgoing messages
    private StringBuffer mOutStringBuffer;
    // Local Bluetooth adapter
    private BluetoothAdapter mBluetoothAdapter = null;
    // Member object for the chat services
	
	
	public BTGameScene(int pLayerCount, Engine baseEngine) {
		super(pLayerCount, baseEngine);
		// TODO Auto-generated constructor stub
	}

	protected void onLoadScene() {

		this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0),
				true, 10, 10);

		this.setOnAreaTouchTraversalFrontToBack();

		this.getChild(0).attachChild((new Sprite(0, 0, this.mBackground)));
		this.getChild(1).attachChild((new Sprite(0, 0, this.mChessboard)));

		mTurnMark[0] = new Sprite(160 - mTurnMarkRgn.getWidth() / 2,
				423 - mTurnMarkRgn.getHeight() / 2, this.mTurnMarkRgn);
		mTurnMark[1] = new Sprite(160 - mTurnMarkRgn.getWidth() / 2,
				57 - mTurnMarkRgn.getHeight() / 2, this.mTurnMarkRgn);
		this.getChild(0).attachChild(mTurnMark[0]);
		this.getChild(0).attachChild(mTurnMark[1]);
		mTurnMark[0].setVisible(false);
		mTurnMark[1].setVisible(false);
		mTurnMark[0].setBlendFunction(GL10.GL_SRC_ALPHA,
				GL10.GL_ONE_MINUS_SRC_ALPHA);
		mTurnMark[1].setBlendFunction(GL10.GL_SRC_ALPHA,
				GL10.GL_ONE_MINUS_SRC_ALPHA);

		// Group1
		createChessman(0.0f, CHESSBOARD_LATTICE_HEIGHT * 3.5f,
				ChessmanSprite.LARGE_SIZE, mDavidsdeerRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.SMALL_SIZE,
				mCrutchRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.SMALL_SIZE,
				mCrutchRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 2.0f,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mHatRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 2.0f,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mHatRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 3.0f,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mSockRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 3.0f,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mSockRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 4.0f,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mHatRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 4.0f,
				CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mHatRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 3.0f,
				CHESSBOARD_LATTICE_HEIGHT * 2.5f, ChessmanSprite.MEDIUM_SIZE,
				mBellRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 3.0f,
				CHESSBOARD_LATTICE_HEIGHT * 2.5f, ChessmanSprite.MEDIUM_SIZE,
				mBellRgn, Brain.GROUP1);
		createChessman(0, CHESSBOARD_LATTICE_HEIGHT * 1.5f,
				ChessmanSprite.SMALL_SIZE, mSnowRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 2.0f,
				CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 2.0f,
				CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP1);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 4.0f,
				CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP1);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 4.0f,
				CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP1);
		// Group2
		createChessman(0.0f, -CHESSBOARD_LATTICE_HEIGHT * 3.5f,
				ChessmanSprite.LARGE_SIZE, mHorseRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.SMALL_SIZE,
				mCrutchRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.SMALL_SIZE,
				mCrutchRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 2.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mLeafRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 2.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mLeafRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 3.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mPresentRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 3.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mPresentRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 4.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mLeafRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 4.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 4.5f, ChessmanSprite.MEDIUM_SIZE,
				mLeafRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 3.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 2.5f, ChessmanSprite.MEDIUM_SIZE,
				mCookiemanRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 3.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 2.5f, ChessmanSprite.MEDIUM_SIZE,
				mCookiemanRgn, Brain.GROUP2);
		createChessman(0, -CHESSBOARD_LATTICE_HEIGHT * 1.5f,
				ChessmanSprite.SMALL_SIZE, mSnowRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 2.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 2.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP2);
		createChessman(CHESSBOARD_LATTICE_WIDTH * 4.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP2);
		createChessman(-CHESSBOARD_LATTICE_WIDTH * 4.0f,
				-CHESSBOARD_LATTICE_HEIGHT * 1.5f, ChessmanSprite.SMALL_SIZE,
				mSnowRgn, Brain.GROUP2);
		this.setTouchAreaBindingEnabled(true);

		createProp(55, 455, mPowerUpPropRgn, Brain.GROUP1, PropSprite.POWERUP);
		createProp(125, 455, mForbidPropRgn, Brain.GROUP1, PropSprite.FORBID);
		createProp(195, 455, mEnlargePropRgn, Brain.GROUP1, PropSprite.ENLARGE);
		createProp(265, 455, mChangePropRgn, Brain.GROUP1, PropSprite.CHANGE);

		createProp(265, 25, mPowerUpPropRgn, Brain.GROUP2, PropSprite.POWERUP);
		createProp(195, 25, mForbidPropRgn, Brain.GROUP2, PropSprite.FORBID);
		createProp(125, 25, mEnlargePropRgn, Brain.GROUP2, PropSprite.ENLARGE);
		createProp(55, 25, mChangePropRgn, Brain.GROUP2, PropSprite.CHANGE);

		mBrain.init();
		mBrain.setGameScene(this);

		MyContactListener listener = new MyContactListener(this);
		// 锟斤拷锟�inge
		int hingeInterval = 75;
		listener._bodyA = createHinge(StartActivity.CAMERA_WIDTH / 2
				- hingeInterval, StartActivity.CAMERA_HEIGHT / 2, mHingeRgn);
		listener._bodyB = createHinge(StartActivity.CAMERA_WIDTH / 2
				+ hingeInterval, StartActivity.CAMERA_HEIGHT / 2, mHingeRgn);
		this.mPhysicsWorld.setContactListener(listener);

		this.mEngine.setScene(this);
		
		this.registerUpdateHandler(new TimerHandler(0.5f, true,
				new ITimerCallback() {
					@Override
					public void onTimePassed(final TimerHandler pTimerHandler) {
						// System.out.println("tick");
						mBrain.checkGameOver();
						mBrain.checkDrop();
						BTGameScene.this.checkTurn();
						if (mBrain.isGameOver == true)
							BTGameScene.this
									.unregisterUpdateHandler(pTimerHandler);
					}
				}));
		this.registerUpdateHandler(new TimerHandler(0.03f, true,
				new ITimerCallback() {
					@Override
					public void onTimePassed(final TimerHandler pTimerHandler) {
						mBrain.drawPropCD();
						turnMarkFadeFunc();
					}
				}));
		this.registerUpdateHandler(this.mPhysicsWorld);
		
		Log.d("my tag", "load game");
		mFirstUpdate = true;

		StartActivity.SCENE_STATE = StartActivity.STATE_GAMESCENT;

	}
    
    public void sendMessage(String message) {
        // Check that we're actually connected before trying anything
        if (BluetoothService.getService().getState() != BluetoothService.STATE_CONNECTED) {
            return;
        }

        // Check that there's actually something to send
        if (message.length() > 0) {
            // Get the message bytes and tell the BluetoothChatService to write
            byte[] send = message.getBytes();
            BluetoothService.getService().write(send);
        }
    }
    
    
    public void HandleMessage(BTMessage msg)
    {
    	switch(msg.packetCodes)
    	{
    	case CHESSMAN_SELECT_EVENT:
    		break;
    	case CHESSMAN_COLLISION_EVENT:
    		break;
    	case CHESSMAN_ENLARGE_EVENT:
    		break;
    	case CHESSMAN_MOVE_EVENT:
    		break;
    	}
    }
    
    
    
    public void handleString(String str)
    {
    	Log.d("handlerString", str);
    }
    
}



